/**
*
*
*
**/
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>
#include<unistd.h>

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<event.h>
#include<event2/util.h>


void cmd_msg_cb(int fd , short events, void *arg)
{
    char msg[1024];
    int ret = read(fd , msg , sizeof(msg));
    int sockfd =  *((int *)arg);
    write(sockfd, msg , ret);
}

void socket_read_cb(int fd, short events , void *arg)
{
    char msg[1024];
    int len = read(fd , msg , sizeof(msg)-1);
    msg[len] = '\0';
    printf("recv %s from server   \n", msg);
}

int main()
{
    int sockfd , status;
    struct  sockaddr_in  server_addr;

    memset(&server_addr ,0 , sizeof(server_addr) );
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(65533);
    inet_aton("127.0.0.1" , &server_addr.sin_addr);
    sockfd = socket(PF_INET , SOCK_STREAM , 0);

    status = connect(sockfd,(sockaddr *)&server_addr , sizeof(server_addr) );
    evutil_make_socket_nonblocking(sockfd);


    event_base *base = event_base_new();

    struct event *ev_sockfd = event_new(base , sockfd , EV_READ| EV_PERSIST, socket_read_cb , NULL );
    event_add(ev_sockfd , NULL);

    struct event *ev_cmd = event_new(base , STDIN_FILENO , EV_READ | EV_PERSIST , cmd_msg_cb , (void *)&sockfd );
    event_add(ev_cmd , NULL);
    event_base_dispatch(base);

}
